package com.company.databind;

import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonIgnoreType;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.deser.std.StringDeserializer;
import com.fasterxml.jackson.databind.module.SimpleModule;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by ruoan on 2020/9/27 16:45
 */
public class JacksonTest {

    //@JsonIgnoreProperties(ignoreUnknown = true)
    //@JsonIgnoreProperties(value = {"name","id"})



    public static void main(String[] args) {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.enable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
        //美化输出
        objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
        //反序列化时候允许null代替“ ”
        objectMapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
        //字符串类型值 允许null代替“ ”
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addDeserializer(String.class, new StdDeserializer<String>(String.class) {
            @Override
            public String deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
                String result = StringDeserializer.instance.deserialize(jsonParser, deserializationContext);
                if (StringUtils.isEmpty(result)) {
                    return null;
                }
                return result;
            }
        });
        // 登记模块
        objectMapper.registerModule(simpleModule);

    //    Fruit fruit = new Fruit();
    //    fruit.code = 1;
    //    fruit.name = "apple";
    //    try {
    //        //序列化
    //        String s = objectMapper.writeValueAsString(fruit);
    //        System.out.println(s);
    //
    //
    //        objectMapper.writeValue(new File("fruit.json"),fruit);
    //        byte[] bytes = objectMapper.writeValueAsBytes(fruit);
    //        System.out.println("字节数组："+bytes);
    //
    //
    //        //反序列化
    //        Fruit fruit1 = objectMapper.readValue(s, Fruit.class);
    //        Fruit fruit2 = objectMapper.readValue(bytes, Fruit.class);
    //        Fruit fruit3 = objectMapper.readValue(new File("fruit.json"), Fruit.class);
    //        System.out.println(fruit1);
    //        System.out.println(fruit2);
    //        System.out.println(fruit3);
    //    } catch (JsonProcessingException e) {
    //        e.printStackTrace();
    //    } catch (IOException e) {
    //        e.printStackTrace();
    //    }
    //
    //
    //
    //
    //
    ////    下面是map和list和json
    //    Map<String, Object> map = new HashMap<>();
    //    map.put("age", 25);
    //    map.put("name", "yitian");
    //    map.put("interests", new String[]{"pc games", "music"});
    //
    //    try {
    //        //写map到json字符串
    //        String s = objectMapper.writeValueAsString(map);
    //        System.out.println(s);
    //
    //        //解析层次分明的json字符串
    //        JsonNode jsonNode = objectMapper.readTree(s);
    //        String name = jsonNode.get("name").asText();
    //        System.out.println(name);
    //        JsonNode interests = jsonNode.get("interests");
    //        String interestsIndex0 = interests.get(0).asText();
    //        System.out.println(interestsIndex0);
    //        //从json字符串到 map
    //        Map<String, Object> stringObjectMap = objectMapper.readValue(s, new TypeReference<Map<String, Object>>() {
    //        });
    //        System.out.println(stringObjectMap);
    //    } catch (JsonProcessingException e) {
    //        e.printStackTrace();
    //    }



        //class T{
        //    //private int id;
        //    //
        //    //public int getId() {
        //    //    return id;
        //    //}
        //    //
        //    //public void setId(int id) {
        //    //    this.id = id;
        //    //}
        //}
        //T t = new T();
        //F f = new F();
        //try {
        //    byte[] bytes = objectMapper.writeValueAsBytes(f);
        //    String s = objectMapper.writeValueAsString(f);
        //    System.out.println(bytes);
        //    System.out.println(s);
        //
        //
        //} catch (JsonProcessingException e) {
        //    e.printStackTrace();
        //}


        try {
            Fruit fruit = objectMapper.readValue(new File("fruit.json"), Fruit.class);
            System.out.println(fruit);
            System.out.println(fruit.getName()==null);
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}


@Data
@NoArgsConstructor
class F{
    private int id;

}
class Fruit{

    private String name;
    //@JsonProperty(value = "id",index = -1)
    private int code;

    public Fruit() {
    }

    public Fruit(String name, int code) {
        this.name = name;
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    @Override
    public String toString() {
        return "Fruit{" +
                "name='" + name + '\'' +
                ", code=" + code +
                '}';
    }
}
